<?php
function anti_spam() {
	$CI = & get_instance ();
	
	$random1 = rand ( 1, 20 );
	$random2 = rand ( 1, 20 );
	
	$total = $random1 + $random2;
	$CI->session->set_userdata ( 'some', $total );
	
	$faq = $random1 . ' + ' . $random2;
	
	return $faq;
}

function path_theme() {
	$path = base_url().'publics/duclinh/';
	return $path;
}

function get_lang() {
	$CI = & get_instance();

	$CI->load->library('session');
	
	$get_lang =  $CI->session->userdata('lang_id');	
		
	if ( $get_lang ) 
		$lang = $get_lang;
	else {
		$CI->session->set_userdata('lang_id', 'vn');
		$lang = 'vn';
	}	
		
	return $lang;	
}

function get_lang_value( $value = 'vn' ) {
	
	if ( !$value ) $value = 'vn';
		
	$list = array(
		'vn' => 1,
		'en' => 2
	);
	
	if ( array_key_exists( $value, $list )) 
	    $lang_value = $list[$value];
	else
		$lang_value = 1;
		
	return $lang_value;
	
}

function path_gallery() {
	$path = base_url () . MYN_UPLOAD_GALLERY;
	return $path;
}

function get_category( $lg = 1, $cat = 0, $type = 0, &$menu ) {
	$CI = & get_instance();
	
	$data = $CI->duclinh_model->get_cat( $lg, $type, $cat);

	if( count($data) > 0 ) {		
		foreach ($data as $dt)	{					
			$menu[] = $dt;				
			get_category( $lg, $dt['id'], $dt['type'], $menu );
		}
	}	
}

function link_cat( $id = 0, $name = '', $namecat ) {
	$path = base_url () . 'cat/'. remove_tv ( $namecat ) . '/' .remove_tv ( $name ).'/'.$id;
	return $path;
}

function link_download( $id = 0, $name = '' ) {
	$path = base_url () . 'download/cat/'. remove_tv ( $name ) .'/'.$id;
	return $path;
}

function link_detail( $id = 0, $type , $name = '' ) {
	$path = base_url () . 'detail/'. $type . '/' .remove_tv ( $name ).'/'.$id;
	return $path;
}


function link_list( $id = 0, $name = '' ) {
	$path = base_url () .'service/cat/'. remove_tv ( $name ).'/'.$id ;
	return $path;
}

function link_news( $id = 0, $name = '' ) {
	$path = base_url () .'news/cat/'. remove_tv ( $name ).'/'.$id ;
	return $path;
}

function remove_tv( $tx = '' ) {
	$tx = filter_vn( $tx );
	//$tx = str_replace( ' ', '-', $tx );		
	//$rv = array('~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '+', '|', ',', 'amp;', '.', ';', '?');
	//$tx = str_replace( $rv, '', $tx );	
	$tx = preg_replace( '/[^a-zA-Z0-9 ]/' , '' , $tx);
	$tx = str_replace( ' ', '-', $tx );
	$tx = strtolower( $tx );
	return $tx;
}

function link_main( $lk ) {
	$path = base_url () . $lk ;
	return $path;
}

/**
 * Sửa lý link
 *
 */
function check_link( $link = '' ) {

	$pos = strpos( $link, 'http://' );
			
	if ($pos !== false) 
		return $link;
	else 
		return link_main( $link );
}

function image_alt( $text ) {
	$text = str_replace( '"', '', $text);
	return $text;
}

function image_thumb($imgname, $type, $height, $width, $alt = '' ) {
	$CI =& get_instance();

	$image_path = 'caches/thumbs/'.$type;
	
	$image_thumb = $image_path . '/' . $height . '_' . $width . '_' . $imgname;

	if( ! file_exists($image_thumb)){
		// LOAD LIBRARY
		$CI->load->library('image_lib');

		// CONFIGURE IMAGE LIBRARY
		$config['image_library']	= 'gd2';
		$config['source_image']		= MYN_UPLOAD_GALLERY.$imgname;
		$config['new_image']		= $image_thumb;
		$config['maintain_ratio']	= TRUE;
		$config['height']			= $height;
		$config['width']			= $width;
		$CI->image_lib->initialize($config);
		$CI->image_lib->resize();
		$CI->image_lib->clear();
	}

	return '<img src="' . base_url() .  $image_thumb . '" alt="'.image_alt( $alt ).'" />';
}

function cat_combo( $array, $curr, $size = 1 ) {
	
		
	echo '<select size="'.$size.'" name="cat" id="cat" class="combo2">';
		echo '<option value="0" selected="selected"> Tất cả </option>';
		if ( $array ) {	
			foreach ( $array as $val ) {				
				echo '<option ';
				if ( $curr == $val['id'] ) echo 'selected="selected"';
				echo 'value="'.$val['id'].'">';
				for( $i=0; $i < (int)$val['lever']; $i++ ) 
                	echo '&mdash;&mdash;';
					
				echo ' '.$val['name'];
				echo '</option>';				
			}
		}
	echo '</select>';
}

?>